/**
* Mp3Player
* @author maze | guavestudios.com
* @version 0.1
*/

package ch.sfug.player {
	import flash.events.Event;	import flash.events.ProgressEvent;	import flash.events.TimerEvent;	import flash.media.SoundChannel;	import flash.media.SoundTransform;	import flash.net.URLRequest;		import ch.sfug.net.loader.AbstractLoader;	import ch.sfug.net.loader.SoundLoader;	
	/**
	 * Mp3 Player<br>
	 * This player is capable of playing music files. 
	 * 
	 * @example The following code shows the usage  <listing version="3.0" >
	 * var player:Mp3Player = new Mp3Player();
	 * player.loader.load(new URLRequest("data/test.mp3")); //its default stopped
	 * player.play();
	 * </listing>
	 * 
	 */
	public class Mp3Player extends AbstractPlayer implements IExternalPlayer,ISoundProvider{
		
		//{ STATIC
		
		//}
		
		//{ MEMBERS
		private var soundLoader:SoundLoader;
		private var channel:SoundChannel;
		private var transform:SoundTransform;
		private var position:uint;
		
		//}
		
		//{ GETTER-SETTER
		public function get loader():AbstractLoader
		{
			return soundLoader;
		}
		//}
		
		//{ EVENT DEFINITION
		
		//}
		
		//{ INIT
		public function Mp3Player() {
			
			
			init();
		}
		private function init():void {
			transform = new SoundTransform();
			position = 0;
			
			soundLoader = new SoundLoader();
			soundLoader.addEventListener(ProgressEvent.PROGRESS, loader_progress);
		}
		//}
		
		//{ PUBLIC METHODS
		public function load(url:URLRequest):void
		{
			
			soundLoader.load(url);
			
			_play();

		}
		//}
		
		//{ OVERRIDEN METHODS
		/**
		 * @inheritDoc
		 */
		internal override function loadPlayer():void {
			
			if (soundLoader.Connected)
				_play();
		}
		
		/**
		 * @inheritDoc
		
		 */
		internal override function stopPlayer():void {
			position = 0;
			_stop();
		}
		
		/**
		 * @inheritDoc
		 */
		internal override function pausePlayer():void {
			position = channel.position;
			
			_stop();
		}
		
		
		/**
		 * @inheritDoc
		 */
		internal override function seekPlayerTo( time:Number ):void {
			_stop();
			position = time;
			_play();
		}
		
		/**
		 * @inheritDoc
		 */
		internal override function resumePlayer(  ):void {
			//soundLoader
			_play();
		}
		/**
		 * @inheritDoc
		 */
		internal override function restartPlayer(  ):void {
			_stop();
			position = 0;
			_play();
		}

		/**
		 * @inheritDoc
		 */
		public override function get duration():Number {
			return  soundLoader.duration;
		}
		
		/**
		 * @inheritDoc
		 */
		public override function get time():Number {
			return channel ? channel.position : 0;
		}
		//}
		
		//{ PRIVATE METHODS
		private function _stop():void
		{
			if (channel) channel.stop();
		}
		private function _play():void
		{
			if (channel) channel.stop();
			channel = soundLoader.getResult().play(position, 0, transform);
			channel.addEventListener(Event.SOUND_COMPLETE, channel_sound_complete);
		}
		public function get soundTransform():SoundTransform {
			return transform;
		}

		
		public function set soundTransform( st:SoundTransform ):void {
			transform = st;
			if(isPlaying())channel.soundTransform = st;
		}
		//}
		
		//{ EVENT HANDLING
		private function playTrigger_timer(e:TimerEvent):void
		{
			position = channel.position;
			
		}
		private function loader_progress(e:ProgressEvent):void
		{
			dispatchEvent(e);
		}
		private function loader_open(e:Event):void
		{
			//supports start if loaded later
			if (isPlaying() && !channel)
				_play();
		}
		private function channel_sound_complete(e:Event):void
		{
			setEndState();
		}
		//}
		
	}
	
}
